package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.MedicalTechnologyMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.MedicalTechnologyRemoteDto;
import com.mk.admin.model.entity.MedicalTechnologyEntity;
import com.mk.admin.model.query.MedicalTechnologyQuery;
import com.mk.admin.model.vo.MedicalTechnologyItemRemoteVo;
import com.mk.admin.model.vo.MedicalTechnologyRemoteVo;
import com.mk.admin.model.vo.MedicalTechnologyVo;
import com.mk.admin.service.MedicalTechnologyService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.HttpTool;
import com.mk.admin.util.JSONCopy;
import com.mk.admin.util.NumberTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class MedicalTechnologyServiceImpl implements MedicalTechnologyService {
    @Autowired
    private MedicalTechnologyMapper medicalTechnologyMapper;


    @Override
    public ResultDto select(MedicalTechnologyQuery query) {
        MedicalTechnologyEntity entity = JSONCopy.copy(query, MedicalTechnologyEntity.class);
        return ResultDto.data(medicalTechnologyMapper.count(entity), medicalTechnologyMapper.select(entity));
    }

    @Override
    public ResultDto add(MedicalTechnologyVo medicalTechnologyVo) {
        MedicalTechnologyEntity entity = JSONCopy.copy(medicalTechnologyVo, MedicalTechnologyEntity.class);
        entity.setMoney(NumberTool.strToMoney(medicalTechnologyVo.getMoney()));
        entity.setCreateTime(DateTool.format());
//        得到远程的申请单号
        entity.setApplyBillNo(remoteReservation(medicalTechnologyVo).getApplyBillNo());
        return ResultDto.data(medicalTechnologyMapper.insert(entity));
    }

    /**
     * 预约的时候需要调用远程预约接口
     *
     * @return
     */
    private MedicalTechnologyRemoteDto remoteReservation(MedicalTechnologyVo medicalTechnologyVo) {
        MedicalTechnologyRemoteVo remoteVo = new MedicalTechnologyRemoteVo();
        remoteVo.setPatId(remoteVo.getPatId());
        remoteVo.setDoctorCode(medicalTechnologyVo.getDoctorCode());
        remoteVo.setBillDeptCode(medicalTechnologyVo.getDepartmentCode());
        remoteVo.setApplyDate(medicalTechnologyVo.getVisitTime());
        List<MedicalTechnologyItemRemoteVo> list = new ArrayList<>();
        MedicalTechnologyItemRemoteVo vo = new MedicalTechnologyItemRemoteVo();
        String type = medicalTechnologyVo.getMedicalType();
        vo.setItemCd(medicalTechnologyVo.getCode());
        vo.setItemName(medicalTechnologyVo.getName());
        vo.setItemType("05".equals(type) ? "1" : "2");
        list.add(vo);
        remoteVo.setItem(list);
        remoteVo.setItemCd(medicalTechnologyVo.getCode());
        remoteVo.setItemName(medicalTechnologyVo.getName());
        remoteVo.setItemType("05".equals(type) ? "1" : "2");
        remoteVo.setExDeptCode(medicalTechnologyVo.getDepartmentCode());
        JSONObject jsonObject = HttpTool.postJson("opt_0054", JSONObject.toJSONString(remoteVo));
        Object result = jsonObject.get("result");
        if (result != null && !"[]".equals(result)) {
            List<MedicalTechnologyRemoteDto> remoteDtoList = JSONArray.parseArray(result.toString(), MedicalTechnologyRemoteDto.class);
            if (remoteDtoList.size() > 0) {
                return remoteDtoList.get(0);
            }
        }
        return new MedicalTechnologyRemoteDto();
    }

    @Override
    public ResultDto update(MedicalTechnologyVo medicalTechnologyVo) {
        MedicalTechnologyEntity entity = JSONCopy.copy(medicalTechnologyVo, MedicalTechnologyEntity.class);
        entity.setMoney(NumberTool.strToMoney(medicalTechnologyVo.getMoney()));
        return ResultDto.data(medicalTechnologyMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(medicalTechnologyMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(medicalTechnologyMapper.status(id));
    }

    @Override
    public ResultDto id(Integer id) {
        return ResultDto.data(medicalTechnologyMapper.selectById(id));
    }

}
